import http from "node:http";
import ContentType from "../base/ContentType";

/**
 * 响应工具
 */
class ResponseUtil {

    /**
     * 设置响应头返回内容为JSON格式
     * @param {http.ServerResponse} response 响应
     */
    setHeaderContentTypeJSON(response: http.ServerResponse) {
        response.setHeader('Content-Type', ContentType.APPLICATION_JSON + '; charset=utf8');
    }

    /**
     * 设置响应头返回内容为Text格式
     * @param {http.ServerResponse} response 响应
     */
    setHeaderContentTypeText(response: http.ServerResponse) {
        response.setHeader('Content-Type', ContentType.TEXT_PLAIN + '; charset=utf8');
    }

    /**
     * 设置响应头返回内容为Html格式
     * @param {http.ServerResponse} response 响应
     */
    setHeaderContentTypeHtml(response: http.ServerResponse) {
        response.setHeader('Content-Type', ContentType.TEXT_HTML + '; charset=utf8');
    }

    /**
     * 设置响应头返回内容为js格式
     * @param {http.ServerResponse} response 响应
     */
    setHeaderContentTypeJS(response: http.ServerResponse) {
        response.setHeader('Content-Type', ContentType.APPLICATION_JS + '; charset=utf8');
    }

    /**
     * 设置响应头返回内容为css格式
     * @param {http.ServerResponse} response 响应
     */
    setHeaderContentTypeCss(response: http.ServerResponse) {
        response.setHeader('Content-Type', ContentType.TEXT_CSS + '; charset=utf8');
    }

    /**
     * 允许跨域请求
     * @param {http.ServerResponse} response 响应
     */
    allowCross(response: http.ServerResponse) {
        response.setHeader('Access-Control-Allow-Methods', '*') //允许哪些请求方式  *代表不限制
        response.setHeader('Access-Control-Allow-Origin', '*') //允许哪些请求域名  *代表不限制
    }

    /**
     * 返回 404
     * @param {http.ServerResponse} response 响应
     */
    writeNotFound(response: http.ServerResponse) {
        response.writeHead(404);
        response.end('The request path does not exist');
    }
}

export default new ResponseUtil();
